package cn.zz.veggie.web.controller.mgmt;

import cn.zz.comm.types.result.R;
import cn.zz.comm.web.annotation.BoxRespResult;
import cn.zz.veggie.application.service.CategoryAppService;
import cn.zz.veggie.web.types.dto.mgmt.MgmtCategoryDto;
import cn.zz.veggie.web.types.dto.mgmt.MgmtCategoryQueryDto;
import cn.zz.veggie.web.types.dto.mgmt.MgmtCategoryStatusDto;
import cn.zz.veggie.web.types.vo.mgmt.MgmtCategoryVo;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 管理端/商品分类管理
 *
 * @author zhongq
 * @since 2024/4/25
 */
@Validated
@BoxRespResult
@RestController
@RequiredArgsConstructor
@RequestMapping("/mgmt/category")
public class MgmtCategoryController {

	private final CategoryAppService categoryAppService;

	/**
	 * 获取所有分类列表
	 */
	@GetMapping
	public List<MgmtCategoryVo> list(@Validated MgmtCategoryQueryDto dto) {
		return categoryAppService.adminFinds(dto);
	}

	/**
	 * 保存分类
	 */
	@PostMapping
	public R<Void> save(@RequestBody @Validated MgmtCategoryDto dto) {
		dto.requireValid();
		return categoryAppService.adminSave(dto);
	}

	/**
	 * 删除分类
	 */
	@DeleteMapping
	public R<Void> delete(@RequestParam("id") @Positive Integer id) {
		return categoryAppService.adminDelete(id);
	}

	/**
	 * 显示分类状态
	 */
	@PutMapping("/status")
	public void changeStatus(@Validated @RequestBody MgmtCategoryStatusDto dto) {
		categoryAppService.adminChangeStatus(dto.getId(), dto.getStatus());
	}

}